import 'package:flutter/material.dart';
import 'package:flutter_learn_demo/category.dart';
import 'package:flutter_learn_demo/home.dart';
import 'package:flutter_learn_demo/search.dart';
import 'package:flutter_learn_demo/settings.dart';

class MainScreenPage extends StatefulWidget {
  final String title;
  const MainScreenPage({super.key, required this.title});
  @override
  State<StatefulWidget> createState() => MainScreenPageState();
}

class MainScreenPageState extends State<MainScreenPage>{

  int _currentIndex = 0;

  late PageController pageController;

  final List<Widget> _pageList = [
    const Home(),
    const Category(),
    const Search(),
    const Settings(),
  ];

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: _currentIndex,keepPage: true);
  }

  @override
  Widget build(BuildContext context) {

    //获取从其他页面传递过来的参数
    var args = ModalRoute.of(context)?.settings.arguments;
    print("args:$args");

    return Scaffold(
        //输入法是否覆盖内容  false--否 悬浮在内容上   true--覆盖内容
        //resizeToAvoidBottomInset: false,
        appBar: AppBar(
            title: const Text(
                "Learn Demo",
                style: TextStyle(
                       fontSize: 25.0,
                       color: Colors.deepOrange,
                ),
                maxLines: 1,
            )
        ),
        body: PageView(
            //禁止左右滑动
            physics: const NeverScrollableScrollPhysics(),
            //可以左右滑动
            //physics: const PageScrollPhysics(),
            controller: pageController,
            children: _pageList,
            onPageChanged: (index) {
              //页面切换回调
            },
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (int index) {
              setState(() {
                  _currentIndex = index;
                  pageController.jumpToPage(_currentIndex);
              });
          },
          iconSize: 40,
          fixedColor: Colors.blue,
          items: const [
             BottomNavigationBarItem(
                 icon: Icon(Icons.home),
                 label: 'Home',
             ),
            BottomNavigationBarItem(
              icon: Icon(Icons.category),
              label: 'Category',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.search),
              label: 'Search',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.settings),
              label: 'Settings',
            ),
          ],
        ),
    );
  }
}